﻿using Minecraft.Config;
using Minecraft.Model;
using Minecraft.Model.Map;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Minecraft.ServerHall.Cmd
{
	public class MapMoveNew : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Map_MapMoveNew;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<MapMoveNewReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：未登录）" });
				return;
			}
			if (req.targetMapId.IsNullOrWhiteSpace())
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：targetMapId 参数为空）" });
				return;
			}
			var map = session.minecraftSessionInfo.map;
			if (req.targetMapId == map.SourceMapId)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：targetMapId 参数值不能==sourceMapId）" });
				return;
			}
			//var targetMap = CsvConfig.mapList.FirstOrDefault(m => m.Id == req.targetMapId);
			var targetMap = CsvConfig.GetMod_MapInfoById(req.targetMapId);
			if (targetMap == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：配置表中不存在该配置信息）" });
				return;
			}
			if (map.isMoving_Convert)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：正处于大地图界面的移动状态，不能进行移动操作）" });
				return;
			}
			//判断targetMapId是否在可视范围内
			var IsWithinVisualField = map.IsWithinVisualField(targetMap.Id);
			if (!IsWithinVisualField)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：targetMapId不在可视范围内）" });
				return;
			}

			session.SetCurPhysicalStrengthValue();

			var timeNow = DateTimeOffset.Now;

			Dictionary<int, DateTime> dict = map.GetTimeTicksDict();

			//var separator1 = "#";
			var separator2 = "|";

			//体力值消耗（目前是固定值，暂不考虑剩余时间的多少而影响体力值的消耗）
			switch (dict.Count())
			{
				case 0:
					{
						//一开始是静止的
						if (req.isRun)
						{
							//var sourceMap = CsvConfig.mapList.FirstOrDefault(m => m.Id == map.SourceMapId);
							var sourceMap = CsvConfig.GetMod_MapInfoById(map.SourceMapId);
							if (sourceMap == null)
							{
								session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：sourceMap信息不存在）" });
								return;
							}
							var mapRoute = map.GetRouteIdList(sourceMap, targetMap, req.isRun);

							//所需体力值
							//var needPhysicalStrengthValue = sourceMap.point3.GetNeedPhysicalStrengthValue(targetMap.point3);
							var needPhysicalStrengthValue = map.GetPhysicalStrengthByRoute(mapRoute);

							//判断体力值是否足够
							if (session.minecraftSessionInfo.player.PhysicalStrengthValue < needPhysicalStrengthValue)
							{
								session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：体力值不够）" });
								return;
							}
							if (session.minecraftSessionInfo.player.PhysicalStrengthValue >= MinecraftConfig.PhysicalStrengthUpperLimit)
							{
								session.minecraftSessionInfo.player.PhysicalStrengthLastGrowTime = timeNow;
							}
							session.minecraftSessionInfo.player.PhysicalStrengthValue -= needPhysicalStrengthValue;
							//加速
							map.TimeTicks = string.Join(separator2, 1, timeNow.Ticks);
						}
						else
						{
							//不加速
							map.TimeTicks = string.Join(separator2, 0, timeNow.Ticks);
						}
					}
					break;
				case 1:
					{
						var first = dict.First();
						if (first.Key == 0)
						{
							//开始是非加速状态
							if (req.isRun)
							{
								session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误10：（原因：非加速状态下不能切换为加速状态）" });
								return;
								////判断体力值是否足够
								//if (session.minecraftSessionInfo.player.PhysicalStrengthValue <= MinecraftConfig.needPhysicalStrengthValue)
								//{
								//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：体力值不够）" });
								//	return;
								//}
								//session.minecraftSessionInfo.player.PhysicalStrengthValue -= MinecraftConfig.needPhysicalStrengthValue;
								////只能传递加速状态值
								//map.timeTicks += separator1 + string.Join(separator2, 1, timeNow.Ticks);
							}
							else
							{
								session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误10：（原因：加速类型非法）" });
								return;
							}
						}
						else
						{
							session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误10：（原因：加速类型非法）" });
							return;
						}
					}
					break;
				default:
					{
						session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误10：（原因：dict.Count() >= 2）" });
						return;
					}
			}

			map.TargetMapId = targetMap.Id;

			var baseResp = map.GetMapNewRespInfo(session, out MapNewRespInfo mapRespInfo);
			if (baseResp.RespLevel != RespLevelEnum.Success)
			{
				session.Send(defCommand, req, baseResp);
				return;
			}


			var resp = new MapMoveNewResp
			{
				mapRespInfo = mapRespInfo
			};
			session.Send(defCommand, req, resp);
		}
	}
}
